<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="initial-scale=1, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
  <title>JRoll下拉组件 for Vue</title>
  <style>
    html,
    body {
      margin: 0;
      padding: 0;
      height: 100%;
      background: #dadada;
    }
    
    header {
      height: 44px;
      line-height: 44px;
      text-align: center;
      background: #da020c;
      color: #fff;
    }
    
    header>a {
      height: 34px;
      line-height: 34px;
      position: absolute;
      top: 5px;
      left: 10px;
    }
    
    h1 {
      margin: 0;
      padding: 0;
      font-size: 18px;
      font-weight: normal;
    }
    
    #wrapper {
      position: absolute;
      top: 44px;
      bottom: 0;
      width: 100%;
    }
    
    #scroller {
      margin: 0;
      padding: 0;
    }
    
    #scroller li {
      height: 60px;
      line-height: 60px;
      border-bottom: 1px solid #ddd;
      text-indent: 1em;
      background: #fff;
    }
    
    #scroller li:nth-last-child(1) {
      border-bottom: none;
    }
  </style>
  <script src="../../vendor/vue.js"></script>
  <script src="../../src/jroll.js"></script>
  <script src="jroll-pulldown.js"></script>
</head>

<body>
  <header>
    <h1>JRoll下拉组件 for Vue</h1>
    <a onclick="javascript:history.back()">&lt;返回</a>
  </header>
  <div id="wrapper">
    <ul id="scroller">
      <li v-for="i in items">{{i}} for Vue</li>
    </ul>
  </div>
  <script>
    //模拟ajax加载
    function ajax(params) {
      setTimeout(function() {
        params.success();
      }, 800);
    }

    /** 注意 **
     * 必须在new JRoll之前 new Vue 
     */
    var vm = new Vue({
        el: '#wrapper',
        data: {
          items: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
        }
    })

    var jroll = new JRoll("#wrapper");
    jroll.pulldown({
      refresh: function(complete) {
        ajax({
          success: function() {
            //异步数据加载完成后必须要执行该complete方法才能结束loading
            complete();

            //每行加20表示已刷新
            var a = []
            vm.$data.items.forEach(function (i) {
              a.push(i + 20)
            })
            //每次刷新多加一条数据
            a.push(a[a.length - 1] + 1)
            vm.$data.items = a

            //scroller高度改变，调用$nextTick方法在Vue更新dom之后刷新JRoll
            vm.$nextTick(function () {
              jroll.refresh()
            })
          }
        });
      }
    });
  </script>
</body>

</html>